package com.hhkj.generator.common;

import java.util.HashMap;
import java.util.Map;

public class JavaMySQLTypeMapper {

    public static final String JAVA_STRING = "String";
    public static final String JAVA_LONG = "Long";
    public static final String JAVA_DOUBLE = "Double";
    public static final String JAVA_INTEGER = "Integer";
    public static final String JAVA_DATE = "Date";
    public static final String JAVA_TIME = "Time";


    public static Map<String, String> sqlJavaMap = new HashMap<>();

    private static Map<String, String> startStringMap = new HashMap<>();

    private static JavaMySQLTypeMapper instance = new JavaMySQLTypeMapper();

    private JavaMySQLTypeMapper() {
        init();
    }

    public static void init() {
        sqlJavaMap.put("date", JAVA_DATE);
        sqlJavaMap.put("datetime", JAVA_DATE);
        sqlJavaMap.put("time", JAVA_TIME);
        sqlJavaMap.put("text", JAVA_STRING);
        sqlJavaMap.put("longtext", JAVA_STRING);

        startStringMap.put("bigint", JAVA_LONG);
        startStringMap.put("varchar", JAVA_STRING);
        startStringMap.put("char", JAVA_STRING);
        startStringMap.put("tinyint", JAVA_INTEGER);
        startStringMap.put("int", JAVA_INTEGER);
        startStringMap.put("decimal", JAVA_DOUBLE);
    }

    public static JavaMySQLTypeMapper getInstance() {
        return instance;
    }

    public String getBySQLType(String sqlType) {
        if (sqlJavaMap.containsKey(sqlType)) {
            return sqlJavaMap.get(sqlType);
        }

        String lowerSqlType = sqlType.toLowerCase();
        for (String key : startStringMap.keySet()) {
            if (lowerSqlType.startsWith(key)) {
                return startStringMap.get(key);
            }
        }

        return JAVA_STRING;
    }

}
